package one.three.twentyFive;

public class InsertAfter {

	private static class Node<Item> {
		private Item item;
		private Node next;

		public boolean insertAfter(Node<Item> a, Node<Item> b) {
			if (a != null && b != null) {
				
				Node<Item> c = this;
				while (c != null) {
					if (c == a) {
						if (a.next != null) {
							b.next = a.next;
						}
						a.next = b;
						return true;
					}
					c = c.next;
				}
			}
			return false;
		}

		@Override
		public String toString() {

			return "[" + item + "]";
		}
	}

	public static void main(String[] args) {
		Node<String> f = new Node<String>();
		f.item = "0";
		Node<String> c = f;
		Node<String> add = new Node<String>();
		add.item = "ADD";
		Node<String> five = null;
		for (int i = 1; i <= 10; i++) {
			c.next = new Node<String>();
			c.next.item = i + "";
			if (i == 5) {
				five = c.next;
			}
			c = c.next;
		}

		c = f;
		while (c != null) {
			System.out.println(c.item);
			c = c.next;
		}

		boolean r=f.insertAfter(five, add);
		System.out.println(r);
		
		c = f;
		
		while (c != null) {
			System.out.println(c.item);
			c = c.next;
			
		}
	
	}

}
